{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Computing basic semantic similarities between GO terms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adapted from book chapter written by _Alex Warwick Vesztrocy and Christophe Dessimoz_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this section we look at how to compute semantic similarity between GO terms. First we need to write a function that calculates the minimum number of branches connecting two GO terms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "load obo file ../go-basic.obo\n",
      "46268"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../go-basic.obo: format-version(1.2) data-version(releases/2016-03-19)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " nodes imported\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys\n",
    "\n",
    "sys.path.insert(0, \"..\")\n",
    "from goatools import obo_parser\n",
    "\n",
    "go = obo_parser.GODag(\"../go-basic.obo\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GO:0048364\tlevel-03\tdepth-04\troot development [biological_process] \n",
      "GO:0044707\tlevel-02\tdepth-02\tsingle-multicellular organism process [biological_process] \n"
     ]
    }
   ],
   "source": [
    "go_id3 = 'GO:0048364'\n",
    "go_id4 = 'GO:0044707'\n",
    "print(go[go_id3])\n",
    "print(go[go_id4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's get all the annotations from arabidopsis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  READ 211727 items: http://geneontology.org/gene-associations/gene_association.tair.gz\n"
     ]
    }
   ],
   "source": [
    "from goatools.associations import read_gaf\n",
    "\n",
    "associations = read_gaf(\"http://geneontology.org/gene-associations/gene_association.tair.gz\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can calculate the semantic distance and semantic similarity, as so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The semantic similarity between terms GO:0048364 and GO:0044707 is 0.25.\n"
     ]
    }
   ],
   "source": [
    "from goatools.semantic import semantic_similarity\n",
    "\n",
    "sim = semantic_similarity(go_id3, go_id4, go)\n",
    "print('The semantic similarity between terms {} and {} is {}.'.format(go_id3, go_id4, sim))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we can calculate the information content of the single term, <code>GO:0048364</code>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Information content (GO:0048364) = 7.75481392334\n"
     ]
    }
   ],
   "source": [
    "from goatools.semantic import TermCounts, ic\n",
    "\n",
    "# First get the counts of each GO term.\n",
    "termcounts = TermCounts(go, associations)\n",
    "\n",
    "# Calculate the information content\n",
    "go_id = \"GO:0048364\"\n",
    "infocontent = ic(go_id, termcounts)\n",
    "print('Information content ({}) = {}'.format(go_id, infocontent))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Resnik's similarity measure is defined as the information content of the most informative common ancestor. That is, the most specific common parent-term in the GO. Then we can calculate this as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resnik similarity score (GO:0048364, GO:0044707) = 4.0540784252\n"
     ]
    }
   ],
   "source": [
    "from goatools.semantic import resnik_sim\n",
    "\n",
    "sim_r = resnik_sim(go_id3, go_id4, go, termcounts)\n",
    "print('Resnik similarity score ({}, {}) = {}'.format(go_id3, go_id4, sim_r))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lin's similarity measure is defined as:\n",
    "$$ \\textrm{sim}_{\\textrm{Lin}}(t_{1}, t_{2}) = \\frac{-2*\\textrm{sim}_{\\textrm{Resnik}}(t_1, t_2)}{IC(t_1) + IC(t_2)} $$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we can calculate this as"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lin similarity score (GO:0048364, GO:0044707) = -0.607721957763\n"
     ]
    }
   ],
   "source": [
    "from goatools.semantic import lin_sim\n",
    "\n",
    "sim_l = lin_sim(go_id3, go_id4, go, termcounts)\n",
    "print('Lin similarity score ({}, {}) = {}'.format(go_id3, go_id4, sim_l))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
